﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Interviewer_HiredApplicant : System.Web.UI.Page
{
    InterviewerMethord IM = new InterviewerMethord();
    string InterviewID = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            InterviewID = Request.QueryString["ID"].ToString();
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        catch (Exception ex)
        {
        }
    }
    private void LoadData()
    {
        DataTable tb = IM.ReturnInterviewInformation(InterviewID);
        if (tb.Rows.Count > 0)
        {
            txtInterviewID.Text = tb.Rows[0]["InterviewID"].ToString();
            txtVacancyID.Text = tb.Rows[0]["VacancyID"].ToString();
            txtApplicantID.Text = tb.Rows[0]["ApplicantID"].ToString();
            txtDate.Text = tb.Rows[0]["DateInterview"].ToString().Substring(0, tb.Rows[0]["DateInterview"].ToString().IndexOf(" "));
            txtTime.Text = tb.Rows[0]["TimeDetails"].ToString();
            switch (IM.ReturnStatusApplicantFollowID(tb.Rows[0]["ApplicantID"].ToString()))
            {
                case "1":
                    btnSave.Visible = false;
                    break;
                default:
                    ddlResult.SelectedIndex = 0;
                    break;
            }
            if (!tb.Rows[0]["Status"].ToString().Equals("1"))
            {
                btnSave.Visible = false;
            }
            else if(Convert.ToDateTime(tb.Rows[0]["DateInterview"].ToString().Substring(0, tb.Rows[0]["DateInterview"].ToString().IndexOf(" "))) >= DateTime.Now)
            {
                btnSave.Visible = false;
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        lblError.Text = "";
        if (ddlResult.SelectedIndex == 1)
        {
            bool result = IM.HiredApplicantForVacancy(txtApplicantID.Text, Convert.ToInt32(txtInterviewID.Text));
            if (result)
            {
                Response.Redirect("SendEmailNoticeHired.aspx?ID=" + txtApplicantID.Text);
            }
            else
            {
                lblError.Text = "Error";
            }
        }
        else
        {
            bool result = IM.UpdateStatusApplicant(txtApplicantID.Text, Convert.ToInt32(txtInterviewID.Text));
            if (result)
            {
                Response.Redirect("ApplicantManager.aspx");
            }
            else
            {
                lblError.Text = "Error";
            }
        }
    }
}